package cn.tedu.store.service.impl;


import cn.hutool.core.util.RandomUtil;
import cn.tedu.store.config.util.MassageUtil;
import cn.tedu.store.config.util.RedisService;
import cn.tedu.store.entity.LisTestReg;
import cn.tedu.store.entity.dto.HospitalSmsDto;
import cn.tedu.store.entity.dto.LisTestRegDto;
import cn.tedu.store.mapper.FuYouLisTestRegMapper;
import cn.tedu.store.mapper.JiKongLisTestRegMapper;
import cn.tedu.store.mapper.PeoPleLisTestRegMapper;
import cn.tedu.store.mapper.ZhongYiLisTestRegMapper;
import cn.tedu.store.service.LisTestRegService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @BelongsProject: zhijiang_hesuan_test
 * @BelongsPackage: com.zhijiang.hospital.service.impl
 * @Author: JunFu
 * @CreateTime: 2022-08-28  18:48
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class LisTestRegServiceImpl implements LisTestRegService {
    @Resource
    PeoPleLisTestRegMapper peoPleLisTestRegMapper;
    @Resource
    JiKongLisTestRegMapper jiKongLisTestRegMapper;
    @Resource
    FuYouLisTestRegMapper fuYouLisTestRegMapper;
    @Resource
    ZhongYiLisTestRegMapper zhongYiLisTestRegMapper;
    @Autowired
    RedisService redisService;
    /**
     * 枝江人民医院核酸查询的接口
     *  先查出是否在医院做过核酸
     *  再来查询结果
     * */
    @Override
    public List<LisTestRegDto> findPeoplehospitalNucleicAcidResults( LisTestRegDto lisTestRegDto) {
        ArrayList<LisTestRegDto> list = new ArrayList<>();
        List<LisTestRegDto> peopleHeSuanList = peoPleLisTestRegMapper.findOnlyPeoPleHospitalHeShuanResult(lisTestRegDto);
        log.info("枝江人民医院核酸查询结果");
        if (peopleHeSuanList.size()!=0)
            list.addAll(peoPleLisTestRegMapper.findPeoPleResults(lisTestRegDto));
        List<LisTestRegDto> jiKongHeShuanResult = jiKongLisTestRegMapper.findOnlyJiKongHospitalHeShuanResult(lisTestRegDto);
        log.info("枝江疾控中心核酸查询结果");
        if (jiKongHeShuanResult.size()!=0)
            list.addAll(jiKongLisTestRegMapper.findJiKongResults(lisTestRegDto));
        List<LisTestRegDto> fuYouHeShuanResult = fuYouLisTestRegMapper.findOnlyFuYouHospitalHeShuanResult(lisTestRegDto);
        log.info("枝江妇幼保健院核酸查询结果");
        if (fuYouHeShuanResult.size()!=0)
            list.addAll(fuYouLisTestRegMapper.findFuYouResults(lisTestRegDto));
        List<LisTestRegDto> zhongYiHeShuanResult = zhongYiLisTestRegMapper.findOnlyZhongYiHospitalHeShuanResult(lisTestRegDto);
        log.info("枝江中医医院核酸查询结果");
        if (zhongYiHeShuanResult.size()!=0)
            list.addAll(zhongYiLisTestRegMapper.findZhongYiResults(lisTestRegDto));
        //if (list.size()==0){
        //    return null;
        //}
        List<LisTestRegDto> collect =
                list.stream().sorted(
                Comparator.comparing(LisTestRegDto::getTestDate).
                reversed()).collect(Collectors.toList());
        return collect;
    }


    /**
     * 使用其他人的身份证号来查核酸结果
     * 要求：用户输入身份证号码，使用用户的身份证号码查询手机号，
     * 然后给对应的手机号码发送短信进行验证
     * 验证通过显示查询结果，验证未通过，则抛出异常信息
     * 继续验证
     * */
    @Override
    public List<LisTestRegDto> findOtherNucleicAcidResults(LisTestRegDto lisTestRegDto) {
        ArrayList<LisTestRegDto> list = new ArrayList<>();
        HospitalSmsDto hospitalSmsDto = new HospitalSmsDto();
        List<LisTestRegDto> peopleLisTestRegDto = peoPleLisTestRegMapper.findPhoneByIdEnNo(lisTestRegDto.getIdEnNo());
        if (peopleLisTestRegDto.size()!=0){
            list.addAll( peoPleLisTestRegMapper.findResult(lisTestRegDto.getIdEnNo()));
            log.info("枝江人民医院核酸查询结果");
        }
        List<LisTestRegDto> jiKongLisTestReg = jiKongLisTestRegMapper.findPhoneByIdEnNo(lisTestRegDto.getIdEnNo());
        if (jiKongLisTestReg.size()!=0){
            list.addAll(jiKongLisTestRegMapper.findResultLisTestRegDtos(lisTestRegDto.getIdEnNo()));
            log.info("疾控核酸查询结果");
        }
        List<LisTestRegDto> fuYouLisTestReg = fuYouLisTestRegMapper.findPhoneByIdEnNo(lisTestRegDto);
        if (fuYouLisTestReg.size()!=0){
            list.addAll(fuYouLisTestRegMapper.findResultLisTestRegDtos(lisTestRegDto.getIdEnNo()));
            log.info("疾控中心的核酸结果");
        }
        List<LisTestRegDto> zhongYiLisTestReg = zhongYiLisTestRegMapper.findPhoneByIdEnNo(lisTestRegDto.getIdEnNo());
        if (zhongYiLisTestReg.size()!=0){
            list.addAll(zhongYiLisTestRegMapper.findResultLisTestRegDtos(lisTestRegDto.getIdEnNo()));
        }
        /**
         * 根据核酸检测结果的时间进行排序
         * */
        List<LisTestRegDto> collect =
                list.stream().sorted(
                Comparator.comparing(LisTestRegDto::getTestDate).
                reversed()).collect(Collectors.toList());
        LisTestRegDto lisTestRegDto1 = collect.get(0);
        hospitalSmsDto.setPhone(lisTestRegDto1.getTelNo());
        String code = RandomUtil.randomNumbers(4);
        String content="您的验证码为"+ code;
        hospitalSmsDto.setContent(content);
        MassageUtil.smsUtil(hospitalSmsDto);
        redisService.setStringKeyAndValue(hospitalSmsDto.getPhone(),code,5);
        String exitCode = redisService.getKeyObjectValue(hospitalSmsDto.getPhone(), String.class);
        hospitalSmsDto.setCode(exitCode);
        log.info(exitCode);
        if(!Objects.equals(hospitalSmsDto.getCode(), exitCode)){
            throw new RuntimeException("当前验证码不存在或者已经过期,请重新获取");
        }
        return list;
    }



    @Override
    public List<LisTestReg> getOneReg(String idenno, String barcode) {
        ArrayList<LisTestReg> list = new ArrayList<>();
        LisTestReg peopleLisTestReg = peoPleLisTestRegMapper.selcetOneReg(idenno, barcode);
        if (("").equals(peopleLisTestReg) && peopleLisTestReg!=null)
            list.add(peopleLisTestReg);
        LisTestReg jiKongLisTestReg = jiKongLisTestRegMapper.selcetOneReg(idenno, barcode);
        if (("").equals(jiKongLisTestReg) && jiKongLisTestReg!=null)
            list.add(jiKongLisTestReg);
        list.add(jiKongLisTestReg);
        LisTestReg fuYouLisTestReg = fuYouLisTestRegMapper.selcetOneReg(idenno, barcode);
        if (("").equals(fuYouLisTestReg) && fuYouLisTestReg!=null)
            list.add(fuYouLisTestReg);
        list.add(fuYouLisTestReg);
        LisTestReg zhongYiLisTestReg = zhongYiLisTestRegMapper.selcetOneReg(idenno, barcode);
        if (("").equals(zhongYiLisTestReg) && zhongYiLisTestReg!=null)
            list.add(peopleLisTestReg);
        list.add(zhongYiLisTestReg);
        return  list;
    }

    @Override
    public LisTestRegDto findNucleicAcidState(LisTestRegDto lisTestRegDto) {
        List<LisTestRegDto>  peopleList = peoPleLisTestRegMapper.findNucleicAcidState(lisTestRegDto);
        List<LisTestRegDto>  fuYouList = fuYouLisTestRegMapper.findNucleicAcidState(lisTestRegDto);
        List<LisTestRegDto>  jiKongList = jiKongLisTestRegMapper.findNucleicAcidState(lisTestRegDto);
        List<LisTestRegDto>  ZhongYiList = zhongYiLisTestRegMapper.findNucleicAcidState(lisTestRegDto);
        peopleList.addAll(fuYouList);
        peopleList.addAll(jiKongList);
        peopleList.addAll(ZhongYiList);
        List<LisTestRegDto> collect = peopleList.stream().sorted(Comparator.comparing(LisTestRegDto::getTestDate).reversed()).
                collect(Collectors.toList());
        if (collect!=null){
            LisTestRegDto lisTestRegDto1 = collect.get(0);
            String memo = lisTestRegDto1.getMemo();
            if (StringUtils.isBlank(lisTestRegDto1.getMemo())||lisTestRegDto1.getMemo()==null){
                lisTestRegDto1.setMemo("试管异常");
            }
            String substring = memo.substring(1, 2);
            if (substring.equals("0")){
                lisTestRegDto1.setMemo("已采样");
            }
            if (substring.equals("2")){
                lisTestRegDto1.setMemo("已核收");
            }
            if (substring.equals("5")){
                lisTestRegDto1.setMemo("已核准");
            }
            if (substring.equals("9")){
                lisTestRegDto1.setMemo("待查");
            }
            return lisTestRegDto1;
        }else {
            return null;
        }

    }


}
